package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> slqw = new LambdaQueryWrapper<>();
        slqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        slqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(slqw);
        return R.success(list);
    }
    //添加购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> slqw = new LambdaQueryWrapper<>();
        slqw.eq(ShoppingCart::getUserId,currentId);
        slqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getUserId,shoppingCart.getDishId());
        slqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartData = shoppingCartService.getOne(slqw);

        if (shoppingCartData!=null){
            shoppingCartData.setNumber(shoppingCartData.getNumber()+1);
            shoppingCartService.updateById(shoppingCartData);
            return R.success(shoppingCartData);
        }else{
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }
    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> slqw = new LambdaQueryWrapper<>();
        slqw.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(slqw);


        return R.success("清空成功");
    }
    //购物车减少
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> slqw = new LambdaQueryWrapper<>();
        slqw.eq(ShoppingCart::getUserId,currentId);
        slqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getUserId,shoppingCart.getDishId());
        slqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart shoppingCartData = shoppingCartService.getOne(slqw);
        shoppingCartData.setNumber(shoppingCartData.getNumber()-1);
        shoppingCartService.updateById(shoppingCartData);
        return R.success(shoppingCartData);

    }
 }   